/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package session;

import entities.Restaurants;
import entities.Rooms;
import java.util.List;
import javax.ejb.Stateless;
import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;
import javax.persistence.Query;

/**
 *
 * @author TANGQUOCTUAN
 */
@Stateless
public class RoomsFacade extends AbstractFacade<Rooms> implements RoomsFacadeLocal {
    @PersistenceContext(unitName = "eProject_Tunga_Restaurant-ejbPU")
    private EntityManager em;

    protected EntityManager getEntityManager() {
        return em;
    }

    public RoomsFacade() {
        super(Rooms.class);
    }

    public List showRoom() {
        Query r=em.createNamedQuery("Rooms.findAll");
        return r.getResultList();
    }

    public List detailRoom(Integer id) {
        Query r = em.createNamedQuery("Rooms.findByRoomID");
        r.setParameter("roomID", id);
        return r.getResultList();
    }

    public List getRoomName() {
       Query r=em.createNamedQuery("Rooms.findAll");
        return r.getResultList();
    }

    public boolean updateRoom(int id, String name,int resID, String description, String picture) {
        Restaurants rs =em.find(Restaurants.class, resID);
        Rooms r = em.find(Rooms.class, id);
        if(r==null) return false;
        r.setRoomName(name);
        r.setRestaurants(rs);
        r.setDescription(description);
        r.setPicture(picture);
        em.persist(r);
        return true;
    }

    public boolean deleteRoom(Integer id) {
        Rooms r = em.find(Rooms.class, id);
        em.remove(r);
        return true;
    }

    public boolean insertRoom(String roomName, int resID, String description, String picture) {
        Restaurants rs =em.find(Restaurants.class, resID);
        Rooms r= new Rooms(roomName, description, picture);
        r.setRestaurants(rs);
        rs.getRoomsList().add(r);
        em.persist(r);
        return true;

    }
}
